<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;

/**
 * 提现记录
 * Class Documentlist
 * @package app\store\controller
 */
class Tixian extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StoreMemberAmountLog';
    protected $table1 = 'store_member_amount_log';

    /**
     * 提现记录
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '提现记录';
        $this->user_list = Db::table("store_member")->field("id,username")->select();

        $query = $this->_query($this->table)->like('memo')->equal("user_id");
        $query->where("is_pay=1 and lx=2")->order('id desc')->page();
    }

    /**
     * 微信转账记录
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function bill()
    {
        $this->title = '微信转账记录';
        $this->id = $_REQUEST['id'];
        $query = $this->_query("store_member_bill")->where(["log_id"=>$_REQUEST['id']]);
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _bill_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['name'] = Db::table("store_member")->where(['openid'=>$vo['openid']])->value('username');
            $vo['tixian_time'] = Db::table("store_member_amount_log")->where(["id"=>$vo['log_id']])->value('create_at');
            if($vo['state']=='WAIT_USER_CONFIRM'){
                $vo['zhuangtai'] = '等待用户收款';
            }elseif ($vo['state']=='SUCCESS'){
                $vo['zhuangtai'] = '用户收款成功';
            }else{
                $vo['zhuangtai'] = '收款已过期或用户取消收款';
            }
        }
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            $u_id = $vo['user_id'];
            $vo['user'] = Db::table("store_member")->where("id=$u_id")->find();
            if ($vo['status'] == 0) {
                $vo['status_r'] = '<span style="color: orangered;font-weight: bold">未审核</span>';
            }
            if ($vo['status'] == 1) {
                $vo['status_r'] = '<span style="color: green;font-weight: bold">同意提现</span>';
            }
            if ($vo['status'] == 2) {
                $vo['status_r'] = '<span style="color: red">拒绝提现</span>';
            }

            $vo['count'] = Db::table("store_member_bill")->where(['log_id'=>$vo['id']])->count();
            $vo['success_count'] = Db::table("store_member_bill")->where(['log_id'=>$vo['id'],'status'=>'已收款'])->count();
            $vo['error_count'] = $vo['count']-$vo['success_count'];
        }


    }

    /**
     * 启用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1', 'audit_at' => date('Y-m-d H:i:s')]);
    }


    /**
     * 启用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume2()
    {
        $find = Db::table($this->table1)->where(['id' => $_REQUEST['id']])->find();
        if ($find) {
            $out_trade_no = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

            $Tixian = new \app\api\controller\Tixian();
            $member_id = $find['user_id'];
            $openid = Db::table("store_member")->where(['id' => $member_id])->value('openid');
            $Tixian->index($openid, $find['amount'] * 100, $out_trade_no,$_REQUEST['id']);

            $row = Db::table("store_member_bill")->field("id")->where(['out_trade_no' => $out_trade_no])->find();
            if ($row) {
                $this->_save($this->table, ['status' => '1','out_trade_no' => $out_trade_no, 'audit_at' => date('Y-m-d H:i:s')]);
            }
        }
    }

    /**
     * 禁用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '2', 'audit_at' => date('Y-m-d H:i:s')]);
    }


}
